//package com.bs.config;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.ProviderManager;
//import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.oauth2.common.OAuth2AccessToken;
//import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
//import org.springframework.security.oauth2.provider.ClientDetailsService;
//import org.springframework.security.oauth2.provider.CompositeTokenGranter;
//import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
//import org.springframework.security.oauth2.provider.TokenGranter;
//import org.springframework.security.oauth2.provider.TokenRequest;
//import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenGranter;
//import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
//import org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter;
//import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;
//import org.springframework.security.oauth2.provider.implicit.ImplicitTokenGranter;
//import org.springframework.security.oauth2.provider.password.ResourceOwnerPasswordTokenGranter;
//import org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter;
//import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
//import org.springframework.security.oauth2.provider.token.*;
//import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
//import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;
//
//import javax.annotation.Resource;
//
//
///**
// * @author bingshao
// * @date 2022/9/14
// **/
//@Configuration
//public class CustomizeAuthorizationServerEndpointsConfigurer {
//
//    private AuthorizationServerTokenServices tokenServices;
//
//    private AuthorizationCodeServices authorizationCodeServices;
//
//    @Resource
//    private TokenStore tokenStore;
//
//    private TokenEnhancer tokenEnhancer;
//
//    @Resource
//    private JwtTokenEnhancer jwtTokenEnhancer;
//
//    private TokenGranter tokenGranter;
//
//    @Resource
//    private JwtAccessTokenConverter jwtAccessTokenConverter;
//
//    private OAuth2RequestFactory requestFactory;
//
//    @Resource
//    private AuthenticationManager authenticationManager;
//
//    @Resource
//    private ClientDetailsService clientDetailsService;
//
//    @Resource
//    private UserDetailsService userDetailsService;
//
//    private boolean reuseRefreshToken = true;
//
//    public TokenEnhancer getTokenEnhancer() {
//        if (this.tokenEnhancer == null) {
//            TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
//            List<TokenEnhancer> delegates = new ArrayList<>();
//            delegates.add(jwtTokenEnhancer);
//            delegates.add(jwtAccessTokenConverter);
//            enhancerChain.setTokenEnhancers(delegates);
//            return enhancerChain;
//        }
//        return this.tokenEnhancer;
//    }
//
//
//    private AuthorizationServerTokenServices tokenServices() {
//        if (tokenServices != null) {
//            return tokenServices;
//        }
//        this.tokenServices = createDefaultTokenServices();
//        return tokenServices;
//    }
//
//    private DefaultTokenServices createDefaultTokenServices() {
//        DefaultTokenServices tokenServices = new DefaultTokenServices();
//        tokenServices.setTokenStore(tokenStore);
//        tokenServices.setSupportRefreshToken(true);
//        tokenServices.setReuseRefreshToken(reuseRefreshToken);
//        tokenServices.setClientDetailsService(clientDetailsService);
//        tokenServices.setTokenEnhancer(getTokenEnhancer());
//        addUserDetailsService(tokenServices, this.userDetailsService);
//        return tokenServices;
//    }
//
//
//    private void addUserDetailsService(DefaultTokenServices tokenServices, UserDetailsService userDetailsService) {
//        if (userDetailsService != null) {
//            PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
//            provider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper<>(
//                    userDetailsService));
//            tokenServices
//                    .setAuthenticationManager(new ProviderManager(Arrays.asList(provider)));
//        }
//    }
//
//
//    private AuthorizationCodeServices authorizationCodeServices() {
//        if (authorizationCodeServices == null) {
//            authorizationCodeServices = new InMemoryAuthorizationCodeServices();
//        }
//        return authorizationCodeServices;
//    }
//
//    private OAuth2RequestFactory requestFactory() {
//        if (requestFactory != null) {
//            return requestFactory;
//        }
//        requestFactory = new DefaultOAuth2RequestFactory(clientDetailsService);
//        return requestFactory;
//    }
//
//
//    private List<TokenGranter> getDefaultTokenGranters() {
//        ClientDetailsService clientDetails = clientDetailsService;
//        AuthorizationServerTokenServices tokenServices = tokenServices();
//        AuthorizationCodeServices authorizationCodeServices = authorizationCodeServices();
//        OAuth2RequestFactory requestFactory = requestFactory();
//
//        List<TokenGranter> tokenGranters = new ArrayList<>();
//        tokenGranters.add(new AuthorizationCodeTokenGranter(tokenServices, authorizationCodeServices, clientDetails,
//                requestFactory));
//        tokenGranters.add(new RefreshTokenGranter(tokenServices, clientDetails, requestFactory));
//        ImplicitTokenGranter implicit = new ImplicitTokenGranter(tokenServices, clientDetails, requestFactory);
//        tokenGranters.add(implicit);
//        tokenGranters.add(new ClientCredentialsTokenGranter(tokenServices, clientDetails, requestFactory));
//        if (authenticationManager != null) {
//            tokenGranters.add(new ResourceOwnerPasswordTokenGranter(authenticationManager, tokenServices,
//                    clientDetails, requestFactory));
//
//            // 自定义短信处理器
//            tokenGranters.add(new SmsTokenGranter(authenticationManager, tokenServices, clientDetails, requestFactory));
//        }
//        return tokenGranters;
//    }
//
//    @Bean
//    public TokenGranter tokenGranter() {
//        if (tokenGranter == null) {
//            tokenGranter = new TokenGranter() {
//                private CompositeTokenGranter delegate;
//
//                @Override
//                public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
//                    if (delegate == null) {
//                        delegate = new CompositeTokenGranter(getDefaultTokenGranters());
//                    }
//                    return delegate.grant(grantType, tokenRequest);
//                }
//            };
//        }
//        return tokenGranter;
//    }
//
//
//}
//
